﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 3/11/2011
-- Description:	News Class
-- =============================================
*/

namespace BusinessLayer
{
   public class News
    {
    #region "Variables and Properties"
    //Variables
        private int news_ID;
        private int news_Type_ID;        
        private string title;
        private string contents;
        private string writer;
        private DateTime writing_Time;
        private bool allow_Comment;
        private int news_Status;

    //Properties
        public int News_ID
        {
            get { return news_ID; }
            set { news_ID = value; }
        }

        public int News_Type_ID
        {
            get { return news_Type_ID; }
            set { news_Type_ID = value; }
        }
        
        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        
        public string Contents
        {
            get { return contents; }
            set { contents = value; }
        }
        
        public string Writer
        {
            get { return writer; }
            set { writer = value; }
        }
        
        public DateTime Writing_Time
        {
            get { return writing_Time; }
            set { writing_Time = value; }
        }
        
        public bool Allow_Comment
        {
            get { return allow_Comment; }
            set { allow_Comment = value; }
        }
        
        public int News_Status
        {
            get { return news_Status; }
            set { news_Status = value; }
        }

    #endregion

    #region "Initial Methods"
        public News() { }

        public News(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                News_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    News_Type_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    Title = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    Contents = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    Writer = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    Writing_Time = sdr.GetDateTime(5);
                if (!sdr.IsDBNull(6))
                    Allow_Comment = sdr.GetBoolean(6);
                if (!sdr.IsDBNull(7))
                    News_Status = sdr.GetInt32(7);
            }
        }
    #endregion

    #region "Business Methods"
    //Get All News
        public static List<News> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_SHOWALL";
            List<News> lstNews=new List<News>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while(sdr.Read())
            {
                lstNews.Add(new News(sdr));
            }

            return lstNews;
        }
    //Get by news type
        public static List<News> GetByType(int typeID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@News_Type_ID", typeID);
            string cmd = "tbl_News_SHOWBYTYPE";
            List<News> lstNews = new List<News>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstNews.Add(new News(sdr));
            }

            return lstNews;
        }
    //Get One News
        public static News GetOne(int idNews)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_SHOWONE";
            News news = new News();
            SqlDataReader sdr;

            dbh.AddParameter("@News_ID", idNews);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                news.News_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    news.News_Type_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    news.Title = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    news.Contents = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    news.Writer = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    news.Writing_Time = sdr.GetDateTime(5);
                if (!sdr.IsDBNull(6))
                    news.Allow_Comment = sdr.GetBoolean(6);
                if (!sdr.IsDBNull(7))
                    news.News_Status = sdr.GetInt32(7);
            }

            return news;
        }

    //Insert News
        public static void Insert(News news)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_INSERT";

            dbh.AddParameter("@News_Type_ID", news.News_Type_ID);
            dbh.AddParameter("@Title", news.Title);
            dbh.AddParameter("@Contents", news.Contents);
            dbh.AddParameter("@Writer", news.Writer);
            dbh.AddParameter("@Writing_Time", news.Writing_Time);
            dbh.AddParameter("@Allow_Comment", news.Allow_Comment);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update News
        public static void Update(News news)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_UPDATE";

            dbh.AddParameter("@News_ID", news.News_ID);
            dbh.AddParameter("@News_Type_ID", news.News_Type_ID);
            dbh.AddParameter("@Title", news.Title);
            dbh.AddParameter("@Contents", news.Contents);
            dbh.AddParameter("@Writer", news.Writer);
            dbh.AddParameter("@Writing_Time", news.Writing_Time);
            dbh.AddParameter("@Allow_Comment", news.Allow_Comment);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete News
        public static void Delete(int idNews)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_News_DELETE";

            dbh.AddParameter("@News_ID", idNews);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
